<?php
include('common-inc.php');

function walker(&$item, $key, $function) {
	switch($function) {
		case 'stripslashes' :
			$item = stripslashes($item);		
		break;
	}
}

#################### Reproduce Start ###########################
if (isset($_POST['re_process']) > 0) {
	$poststring = stripslashes($_POST['poststring']);
	$test_array = unserialize($poststring);	
	
	// Assign reproduce array to POST
	$_POST = $test_array;
}

// Assigning each post to reproduce box 
$poststring = $_POST;
array_walk_recursive($poststring, 'walker', 'stripslashes');	
$poststring = serialize($poststring);
#################### Reproduce End ###########################


if (!empty($_POST['preferences']))
	$preferences = $_POST['preferences'];
else
	$preferences = array();

unset($_POST['poststring']);
//print_r($_POST);
//print_r($preferences);

if (!empty ($_POST['process_fields'])) {
	
	$section_name = ucwords(str_replace('_', ' ', $_POST['name']));
	$section_name_small = strtolower($section_name);
	
	$generate_catalog_controller = (in_array('catalog_controller', $preferences)) ? 1:0;
	$generate_catalog_model = (in_array('catalog_model', $preferences)) ? 1:0;
	$generate_catalog_language = (in_array('catalog_language', $preferences)) ? 1:0;
	$generate_catalog_view = (in_array('catalog_list', $preferences)) ? 1:0;		
	$generate_admin_controller = (in_array('admin_controller', $preferences)) ? 1:0;
	$generate_admin_model = (in_array('admin_model', $preferences)) ? 1:0;
	$generate_admin_language = (in_array('admin_language', $preferences)) ? 1:0;
	$generate_admin_view_list = (in_array('admin_list', $preferences)) ? 1:0;
	$generate_admin_view_form = (in_array('admin_form', $preferences)) ? 1:0;
	
	$safe_delete = (in_array('safe_delete', $preferences)) ? 1:0;
	$debug_mode = (in_array('debug_mode', $preferences)) ? 1:0;

	
	$controller_name = str_replace('_', ' ', $_POST['name']);
	$controller_name = ucwords($controller_name);
	$controller_name = str_replace(' ', '', $controller_name);
	
	$controller_name = ucfirst($_POST['path']) . $controller_name;
	$module_name = $_POST['name']; //ucfirst($_POST['name'])
	$module_name_small = strtolower($_POST['name']);
	$module_table_name = strtolower($_POST['tname']);
	$model_path = strtolower($_POST['path'] . '/' . $module_name_small);
	$module_path = strtolower($_POST['path'] . '/' . $module_name_small);
	$module_fields = $_POST['all_fields'];
	
	$module_primary_key = "";
	foreach($module_fields as $each_key) {
		if($each_key['pk'])
		$module_primary_key = $each_key['name'];
	}
	
	if(empty($module_primary_key))
		$module_primary_key = $module_name_small."_id";
	
	//	echo $module_primary_key;
	//	print_r($module_fields);
	//	die;
	
	######################### Start Catalog Controller #############################
	if ($generate_catalog_controller) {
		$catalog_controller_file_contents = file_get_contents('catalog/controller.php');
		//	echo $catalog_controller_file_contents;
		
		include('catalog/process_controller.php');
		
//		echo $catalog_controller_file_contents;
		
		if($debug_mode) {
			$catalog_controller_file = "output/controller.php";
			file_put_contents($catalog_controller_file, $catalog_controller_file_contents);
		}			
	}
	
	if ($generate_admin_controller) {
		$controller_file_contents = file_get_contents('admin/controller.php');
		//	echo $controller_file_contents;
		
		include('admin/process_controller.php');
	
//		echo $controller_file_contents;
		if($debug_mode) {
			$controller_file = "output/admin_controller.php";
			file_put_contents($controller_file, $controller_file_contents);
		}
	}
	
	######################### End Controller #############################

	######################### Start Model #############################
	if ($generate_catalog_model) {
		$catalog_model_file_contents = file_get_contents('catalog/model.php');

		//	print_r($module_fields);
		
		include('catalog/process_model.php');	

//		echo $catalog_model_file_contents;
		
		if($debug_mode) {
			$catalog_model_file = "output/model.php";
			file_put_contents($catalog_model_file, $catalog_model_file_contents);
		}
	}
	
	if ($generate_admin_model) {
		if($safe_delete)
			$model_file_contents = file_get_contents('admin/model-safe.php');
		else
			$model_file_contents = file_get_contents('admin/model.php');
		

		//	print_r($module_fields);
		
		include('admin/process_model.php');	

//		echo $model_file_contents;
		
		if($debug_mode) {
			$model_file = "output/admin_model.php";
			file_put_contents($model_file, $model_file_contents);
		}
	}
	######################### End Model ###############################	

	######################### Start Language ###############################
	if ($generate_catalog_language) {
		$catalog_language_file_contents = file_get_contents('catalog/language.php');

		//	print_r($module_fields);
		
		include('catalog/process_language.php');			

//		echo $model_file_contents;
		
		if($debug_mode) {
			$catalog_language_file = "output/language.php";
			file_put_contents($catalog_language_file, $catalog_language_file_contents);
		}
	}
	
	if ($generate_admin_language) {
		$language_file_contents = file_get_contents('admin/language.php');

		//	print_r($module_fields);
		
		include('admin/process_language.php');			

//		echo $model_file_contents;
		
		if($debug_mode) {
			$language_file = "output/admin_language.php";
			file_put_contents($language_file, $language_file_contents);
		}
	}
	######################### End Language ###############################	

	######################### Start Front View ###############################
	if ($generate_catalog_view) {
		$catalog_view_file_contents = file_get_contents('catalog/view.tpl');
		
		include('catalog/process_view.php');		

//		echo $catalog_view_file_contents;

		if($debug_mode) {
			$catalog_view_file = "output/view.tpl";
			file_put_contents($catalog_view_file, $catalog_view_file_contents);
		}
	}
	######################### End Front View ###############################

	######################### Start List ###############################
	if ($generate_admin_view_list) {
		$list_file_contents = file_get_contents('admin/view_list.tpl');
		
		include('admin/process_view_list.php');		

//		echo $list_file_contents;
		
		if($debug_mode) {
			$list_file = "output/view_list.tpl";
			file_put_contents($list_file, $list_file_contents);
		}
	}
	######################### End List #################################
	
	######################### Start Form ###############################
	if ($generate_admin_view_form) {
		$form_file_contents = file_get_contents('admin/view_form.tpl');

		include('admin/process_view_form.php');

//		echo $form_file_contents;
		
		if($debug_mode) {
			$form_file = "output/view_form.tpl";
			file_put_contents($form_file, $form_file_contents);
		}
	}
	######################### End Form ###############################

	$file_path = $_POST['path'];
	
	//print_r($_POST);
	
	if (!empty ($file_path)) {
		$zipFileName = 'ocadmin_' . $module_name_small . ".zip";
		
		$zipFolder = "output"; // must be writable
		$zipArchive = $zipFolder . "/" . $zipFileName;
		
		if(is_file($zipArchive)) {
			unlink($zipArchive);
		}			

		// Create the Zip
		$zip = new ZipArchive();
		$zip->open($zipArchive, ZipArchive :: CREATE);

		$zip->addFromString($controller_name.'_mmcode.txt', $poststring);
		
		if($debug_mode) {
			$mm_post_code = "output/".$controller_name.'_mmcode.txt';
			file_put_contents($mm_post_code, $poststring);
		}
		
		// Catalog Files
		if($generate_catalog_controller)
		$zip->addFromString('catalog/controller/' . $file_path . '/' . $module_name_small . '.php', $catalog_controller_file_contents);
		
		if($generate_catalog_model)
		$zip->addFromString('catalog/model/' . $file_path . '/' . $module_name_small . '.php', $catalog_model_file_contents);
		
		if($generate_catalog_language)
		$zip->addFromString('catalog/language/english/' . $file_path . '/' . $module_name_small . '.php', $catalog_language_file_contents);
		
		if($generate_catalog_view)
		$zip->addFromString('catalog/view/template/' . $file_path . '/' . $module_name_small . '.tpl', $catalog_view_file_contents);
		
		// Admin Files
		if($generate_admin_controller)
		$zip->addFromString('admin/controller/' . $file_path . '/' . $module_name_small . '.php', $controller_file_contents);
		
		if($generate_admin_model)
		$zip->addFromString('admin/model/' . $file_path . '/' . $module_name_small . '.php', $model_file_contents);
		
		if($generate_admin_language)
		$zip->addFromString('admin/language/english/' . $file_path . '/' . $module_name_small . '.php', $language_file_contents);
		
		if($generate_admin_view_list)
		$zip->addFromString('admin/view/template/' . $file_path . '/' . $module_name_small . '_list.tpl', $list_file_contents);
		
		if($generate_admin_view_form)
		$zip->addFromString('admin/view/template/' . $file_path . '/' . $module_name_small . '_form.tpl', $form_file_contents);		
		
		$zip->close();

		// Download the Zip
		ob_flush();
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename="' . $zipFileName . '"');
		header('Content-Transfer-Encoding: binary');
		readfile($zipArchive);
		exit;
	}
}

$order_list = array ();
if (isset ($_POST['order_list'])) {
	$order_list = $_POST['order_list'];
}

$order_form = array ();
if (isset ($_POST['order_form'])) {
	$order_form = $_POST['order_form'];
}

$all_fields = array ();
if (isset ($_POST['all_fields'])) {
	$all_fields = $_POST['all_fields'];
}

if (!isset ($_POST['all_fields']) || isset ($_POST['process_again'])) {
	$all_fields = array ();
	$_POST['order_form'] = array ();
	$_POST['order_list'] = array ();
	$post_fields = $_POST['fields'];

	$primary_key = "";
	preg_match('@PRIMARY KEY \(`([^`]+)`\)@', $post_fields, $matches);
	$primary_key = $matches['1'];
	
	$table_name = "";
	preg_match('@ TABLE `([^`]+)`@', $post_fields, $matches);
	$table_name = $matches['1'];
	if(empty($_POST['tname']))
		$_POST['tname'] = $table_name; 
		
	preg_match_all('@`([^`]+)` ([^ \()]+)@', $post_fields, $matches);

	if (count($matches[0]) > 0)
		foreach ($matches[0] as $key => $value) {

			$pk = 0;
			if ($matches[1][$key] == $primary_key)
				$pk = 1;

			$all_fields[] = array (
				'label' => make_name($matches[1][$key]), 
				'name' => $matches[1][$key], 
				'type' => match_type($matches[2][$key], $matches[1][$key]),
				//			'limit' => stripslashes($matches[3][$key]), 
				'pk' => $pk,);
		
			if(empty($_POST['order_form'])) {				
				$order_form[$key] = ($key + 1);
			}
			
			if(empty($_POST['order_list'])) {				
				$order_list[$key] = ($key + 1);
			}
		}

	//	echo "<pre>";
//		print_r($all_options);
//		print_r($all_fields);
//		print_r($order_form);
	//	echo "</pre>"; 

}

$total_fields = count($all_fields);
$add_fields = (int) $_POST['add_new'];

//echo $add_fields;

if($add_fields > 0) {
		
	$start_count = $total_fields + 1;
	$end_count = $start_count + $add_fields - 1;
 	
// 	echo $start_count." - ".$end_count;
 	 
	foreach(range($start_count, $end_count) as $key => $each_field) {
		
		$key = $each_field - 1;
		
		$pk = 0;
		if($each_field == 1 && count($all_fields) == 0) {			
			$primary_key = "id";
			$pk = 1;
		}
			
		$all_fields[] = array (
			'label' => 'Lable', 
			'name' => ($pk) ? 'id' : 'name', 
			'type' => 'text', 
			'pk' => $pk,
		);
		
						
		$order_form[$key] = ($key + 1);
		$order_list[$key] = ($key + 1);
	}
}

$total_fields = count($all_fields);
?>
<style>
.even {
	background:white;
}
.odd {
	background:lightgray;
}

textarea, input[type=text] {
	background:white;
	border:1px solid black;
	color:green;
}

.other_options {
	color:gray;
	font-size:12px;
}
</style>
<link rel="stylesheet" href="css/themes/base/jquery.ui.all.css">
<script src="js/jquery-1.6.2.min.js"></script>
<script src="js/jquery-ui-1.8.16.custom.min.js"></script>
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
.order-lable li {background-color:black; border-color:black !important;}
.list-lable li {background-color:#A52A2A; border-color:#A52A2A !important;}
.form-lable li {background-color:green; border-color:green !important;}
.sortable {margin:0;padding:0;}
.sortable li, .connectedSortable {color: white; font-size:12px;margin:0 5px 5px 5px;padding:5px;border:1px solid red;list-style:none;float:left;}
.ui-state-default { background-color:lightgray; }
</style>
<script>
function check_labels() {
	check_list_order();
	check_form_order();
}
	
function check_list_order() {
	var result = $('#list_order').sortable('toArray');
	$(result).each(function (i) {
		var key = result[i], id= key - 1, value = i + 1;
//		alert(id+" | "+key+" | "+value); 
		$('#order_list_'+id).val(value);
	});
	
	$('#list_order li').each(function (i) {
		var id = this.id - 1;
		$(this).html($('#label_'+id).val());
		if($('#options_'+id+'_0').is(':checked')) {
			$(this).css({'background-color':'#A52A2A'});
		} else {
			$(this).css({'background-color':'lightgray'});
		}
	});
}

function check_form_order() {
	var result = $('#form_order').sortable('toArray');
	$(result).each(function (i) {
		var key = result[i], id= key - 1, value = i + 1;
//		alert(id+" | "+key+" | "+value); 
		$('#order_form_'+id).val(value);
	});
	
	$('#form_order li').each(function (i) {
		var id = this.id - 1;
		
		$(this).html($('#label_'+id).val());
		
		if($('#options_'+id+'_3').is(':checked')) {
			$(this).css({'background-color':'green'});
		} else {
			$(this).css({'background-color':'lightgray'});
		}
	});
} 

var draggingActive = 0;
var draggingActiveTrigger = 0;

jQuery(document).ready(function(){	
	$("#list_order").sortable({
		start: function () { clearTimeout(draggingActiveTrigger); draggingActive = 1; },
		stop: function () { draggingActiveTrigger = setTimeout("draggingActive = 0;", 1000); },
		update: function (event, ui) { check_list_order(); }
	});
	
	$("#form_order").sortable({
		start: function () { clearTimeout(draggingActiveTrigger); draggingActive = 1; },
		stop: function () { draggingActiveTrigger = setTimeout("draggingActive = 0;", 1000); },
		update: function (event, ui) { check_form_order(); }
	});
	
	$("#list_order, #form_order").disableSelection();
	
});
</script>
<h1></h1>
<form id="struct" name="struct" method="post" action="#">
  <div style="float:left">
  <label>Fields :</label> <input type="submit" name="process_again" onclick="validate()" value="Process Fields" />
  &nbsp; <input type="text" name="add_new" value="0" size="5" /> <input type="submit" name="add_submit" value="Add | Reload" /><br />
  <textarea name="fields" id="fields" rows="5" cols="50"><?=stripslashes($_POST['fields']);?></textarea>
  </div>
  <div style="float:right; margin-right:5%;">
  <label>Fields :</label> <input type="submit" name="re_process" onclick="validate()" value="Re-process Fields" /><br />
  <textarea name="poststring" id="poststring" rows="5" cols="50"><?=$poststring;?></textarea>
  </div>  	
  <div style="clear:both"></div>
  <br />
<?php if(count($all_fields) > 0) { ?>
  <table width="95%" border="0" cellpadding="3" cellspacing="0">
  <tr class="odd">
  	<th align="center">S.No</th>
    <th align="left">Name</th>
    <th align="left">DB Field</th>
    <th>Type</th>
    <th>&nbsp;</th>
    <th>Options</th>
    <th>&nbsp;</th>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><small>List</small></td>
    <td nowrap="nowrap"><?php foreach($all_options as $key => $each_option) {?>
  		<span style="font-size:10px;"><a href="javascript:void(0)" style="text-decoration:none;" onclick="toggle_option(<?=$key?>)"><?=$each_option['text'];?></a> | </span>
  	<?php } ?>
	</td>
	<td><small>Form</small></td>
  </tr>
  <?php foreach($all_fields as $key => $each_field) { ?>    
  <tr <?=(($key + 1)%2) ? ' class="even" ':' class="odd" '?>>
    <td align="center"><?=($key + 1);?> <?=($all_fields[$key]['name'] == $primary_key) ? '*':'';?></td>
    <td align="left"><label>
      <input onblur="check_labels();" tabindex="<?=(($total_fields * 0) + 1)?>" name="all_fields[<?=$key?>][label]" type="text" id="label_<?=$key?>" value="<?=$all_fields[$key]['label'];?>" />  	
    </label></td>
    <td align="left"><input tabindex="<?=(($total_fields * 1) + 1)?>" style="color:#A52A2A; width:150px;" name="all_fields[<?=$key?>][name]" type="text" id="name_<?=$key?>" value="<?=$all_fields[$key]['name'];?>" /> <small>[<?=$all_fields[$key]['type']?>]</small> 
    	<input name="all_fields[<?=$key?>][pk]" type="hidden" id="pk_<?=$key?>" value="<?=$all_fields[$key]['pk'];?>" /></td>
    <td align="center"><label>
      <select tabindex="<?=(($total_fields * 2) + 1)?>" name="all_fields[<?=$key?>][type]" id="type_<?=$key?>">
      	<?php foreach($all_types as $each_type) {?>
      	<?php if($each_type['value'] == $all_fields[$key]['type']) {?>
      	<option value="<?=$each_type['value'];?>" selected="selected"><?=$each_type['text'];?></option>
      	<?php } else {?>
      	<option value="<?=$each_type['value'];?>"><?=$each_type['text'];?></option>	
      	<?php } ?>	
      	<?php } ?>        
      </select>
    </label></td>
    <td align="left"><label>
      <input tabindex="<?=(($total_fields * 3) + 1)?>" style="text-align:center" class="list_order" name="order_list[<?=$key?>]" type="text" id="order_list_<?=$key?>" value="<?=$order_list[$key];?>" size="2" />
    </label></td>
    <td align="left" nowrap="nowrap">
		<?php foreach($all_options as $option_key => $each_option) {?>
      	<?php if(in_array($each_option['value'], (array) $all_fields[$key]['options']) || empty($all_fields[$key]['options'])) {?>
      	<span style="margin:0 7px"><input id="options_<?=$key.'_'.$option_key?>" onclick="check_others(this.id)" type="checkbox" name="all_fields[<?=$key?>][options][]" value="<?=$each_option['value'];?>" checked="checked" /></span>
      	<?php } else {?>
      	<span style="margin:0 7px"><input id="options_<?=$key.'_'.$option_key?>" onclick="check_others(this.id)" type="checkbox" name="all_fields[<?=$key?>][options][]" value="<?=$each_option['value'];?>" /></span>	
      	<?php } ?>	
      	<?php } ?></td>
    <td align="left"><label>
      <input tabindex="<?=(($total_fields * 4) + 1)?>" style="text-align:center" class="form_order" name="order_form[<?=$key?>]" type="text" id="order_form_<?=$key?>" value="<?=$order_form[$key];?>" size="2" />
    </label></td>
  </tr>
  <?php } ?>
  <tr>
    <td colspan="7">&nbsp;</td>
  </tr>
<!--
  <tr>
  	<td colspan="2">Field Order</td>
    <td colspan="5">
   		<ul class="sortable order-lable">
   			<?php foreach($all_fields as $key => $each_field) { ?>
			<li><?=$order_list[$key];?></a></li>
			<? } ?>
		</ul>
	</td>
  </tr>
-->  
  <tr>
    <td colspan="7"><small>List Order</small></td>
  </tr>
  <tr>  	
    <td colspan="7">
   		<ul class="sortable list-lable" id="list_order">
   			<?php foreach($all_fields as $key => $each_field) { ?>
			<li id="<?=$order_list[$key]?>" onclick="check_option(<?=$order_list[$key]?>, 0)"><?=$all_fields[$key]['label'];?></li>
			<? } ?>
		</ul>
	</td>
  </tr>
  <tr>
  	<td colspan="7"><small>Form Order</small></td>
  </tr>
  <tr>
  	<td colspan="7">
   		<ul class="sortable form-lable" id="form_order">
   			<?php foreach($all_fields as $key => $each_field) { ?>
			<li id="<?=$order_list[$key]?>" onclick="check_option(<?=$order_list[$key]?>, 3)"><?=$all_fields[$key]['label'];?></li>
			<? } ?>
		</ul>
	</td>
  </tr>
  <tr>
    <td colspan="7">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">Catalog</td>
    <td><a href="javascript:void(0)" onclick="toggle_preference('catalog')" style="font-size: 12px; margin: 0pt 4px;">Check All</a></td>
    <td>Admin</td>
    <td colspan="2"><a href="javascript:void(0)" onclick="toggle_preference('admin')" style="font-size: 12px; margin: 0pt 4px;">Check All</a></td>
  </tr>
  <?php for($key=0; $key < count($other_options); $key+=2) {?>
  <tr>
    <td colspan="2"><span class="other_options"><?=(!empty($other_options[$key]['text'])) ? $other_options[$key]['text']:''?></span></td>
    <td><?php if(!empty($other_options[$key]['text'])) { 
    		  if(in_array($other_options[$key]['value'], (array) $preferences) || empty($preferences)) {?>
      	<input id="preferences_<?=$key?>" type="checkbox" name="preferences[]" value="<?=(!empty($other_options[$key]['text'])) ? $other_options[$key]['value']:''?>" checked="checked" />
      	<?php } else if(!empty($other_options[$key]['text'])) {?>
      	<input id="preferences_<?=$key?>" type="checkbox" name="preferences[]" value="<?=(!empty($other_options[$key]['text'])) ? $other_options[$key]['value']:''?>" />	
      	<?php } } else { echo '&nbsp;'; } ?></td>
    <td><span class="other_options"><?=(!empty($other_options[($key+1)]['text'])) ? $other_options[($key+1)]['text']:''?></a></td>
    <td><?php if(!empty($other_options[($key+1)]['text'])) {
    		  if(in_array($other_options[($key+1)]['value'], (array) $preferences) || empty($preferences)) {?>
      	<input id="preferences_<?=($key+1)?>" type="checkbox" name="preferences[]" value="<?=(!empty($other_options[($key+1)]['text'])) ? $other_options[($key+1)]['value']:''?>" checked="checked" />
      	<?php } else if(!empty($other_options[$key]['text'])) {?>
      	<input id="preferences_<?=($key+1)?>" type="checkbox" name="preferences[]" value="<?=(!empty($other_options[($key+1)]['text'])) ? $other_options[($key+1)]['value']:''?>" />	
      	<?php } } else { echo '&nbsp;'; }  ?></td>
  </tr>
  <?php } ?>
  <tr>
    <td colspan="7">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><small>Module Name :</small></td>
    <td colspan="3"><input name="name" type="text" id="name" value="<?=$_POST['name'];?>" size="25" /> 
    	<input type="button" name="download" value="Download" onclick="mm_download();" /></td>
  </tr>
  <tr>
    <td colspan="2"><small>Table Name :</small></td>
    <td colspan="3"><input name="tname" type="text" id="tname" value="<?=$_POST['tname'];?>" size="25" /></td>
  </tr>
  <tr>
    <td colspan="2"><small>Folder Name :</small></td>
    <td colspan="3"><input name="path" type="text" id="path" value="<?=$_POST['path'];?>" size="50" /><br />
    	<span style="font-size:smaller;color:gray;">e.g: catalog/view/theme/default/image</span></td>
  </tr>
</table><br /><br />
<?php } ?>
	<input type="hidden" id="process_fields" name="process_fields" value="" />
</form>
<div id="log"></div>
<script language="javascript">
function mm_download() {
	document.struct.target = '_blank';
	var process_ele = document.getElementById('process_fields');
	process_ele.value = "Download";
	document.struct.submit();
}

function toggle_preference(check_section) {
	switch(check_section) {
		case 'catalog':
			var catalog_opt = new Array(0,2,4,6,8);
		break;
		case 'admin':
			var catalog_opt = new Array(1,3,5,7,9);
		break;
		default:
			var catalog_opt = new Array(0);
		break;
	}
	 
	
	var check_ele = document.getElementById('preferences_'+catalog_opt[0]).checked;
		
	for(i=0; i < catalog_opt.length ; i++) {
		if(check_ele) {
			document.getElementById('preferences_'+catalog_opt[i]).checked = false;
		} else {
			document.getElementById('preferences_'+catalog_opt[i]).checked = true;
		}
	}
}

function toggle_option(option_num) {
	var total_fields = <?=count($all_fields)?>;
	var check_ele = document.getElementById('options_0_'+option_num).checked;
	
	for(i=0; i < total_fields ; i++) {
		
		if(check_ele) {
			document.getElementById('options_'+i+'_'+option_num).checked = false;
		} else {
			document.getElementById('options_'+i+'_'+option_num).checked = true;
		}			
	}
	
	check_labels();
}

function check_option(row_id, column_id) {
	
	if(draggingActive)
		return false;
		
	ele_id = row_id - 1;
	var option = 'options_'+ele_id+'_'+column_id;
	
	if($('#'+option).is(':checked'))
		$('#'+option).attr('checked', false);
	else
		$('#'+option).attr('checked', true);
		
	check_others(option);
//	alert(option);
}

function check_others(ele_id) {
	check_labels();
	
	var temp_arr = new Array();
	var curr_ele = document.getElementById(ele_id);
	
//	alert(curr_ele.checked+' | '+ele_id);
		
	temp_arr = ele_id.split('_');
	var list_ele = document.getElementById('options_'+temp_arr[1]+'_0');
	var form_ele = document.getElementById('options_'+temp_arr[1]+'_3');
	
	if(! list_ele.checked) {
		document.getElementById('options_'+temp_arr[1]+'_1').checked = false;
		document.getElementById('options_'+temp_arr[1]+'_2').checked = false;
	}
	
	if(! form_ele.checked) {
		document.getElementById('options_'+temp_arr[1]+'_4').checked = false;
	}
}
</script>